
#include "Debug.h"
#include <sstream>
#include <string>
#include <stdlib.h>
#include <sys/stat.h>
#include <mysql.h>
#include <map>
#include <string.h>
#include <glib.h>
#include "config.h"
#include "define.h"

using namespace std;

extern "C" {
  bool simpleConsInit(char *consumer, const char* conf);
  bool simpleConsFriends(char *user, GHashTable *frs);
  void simpleConsShutdown();
  void simpleConsFlush(char *consumer);
};

int g_grade = 0;
int g_expire = 0;
string g_txLog;
bool g_tx_enable = false;

bool simpleConsInit(char *consumer, const char* conf)
{
        g_grade = 0;
        g_expire = 0;
        struct stat st;
        if (lstat(conf, &st) != 0)
        {
                ERRO<<":"<<conf<<" is not exist!"<<endl;
                return false;
        }
        Configure cfg;
        map <string, string > g_Confs;
        g_Confs = cfg.LoadConfInfo(conf);
        cfg.Print();
        if(g_Confs.size() <= 0)
        {
                cerr<<"error to explain conf :"<< conf<<endl;
                return true;
        }
        string sval;
        if( g_Confs.find("expire") != g_Confs.end())
        {
        sval = g_Confs["expire"];
        }
        else
           cout<<"NO expire!"<<endl;
        g_expire  = atoi(sval.c_str());
        if(g_expire < 1)
                g_expire = 1;
        sval = "";
        map <string, string >::iterator iter;
        if (g_Confs.find("grade") != g_Confs.end() )
        {
              sval = g_Confs["grade"];
        }
        g_grade = atoi(sval.c_str());
        if(g_grade < 1)
                g_grade = 1;

        if (g_Confs.find("txlog") != g_Confs.end())
                g_txLog = g_Confs["txlog"];

        cout<<"expire = "<< g_expire << ": grade = " << g_grade << endl;
        if (lstat(g_txLog.c_str(), &st) != 0)
        {
                INFO<<"yreplDbTX is false!"<<endl;
                g_tx_enable = false;
        }
        else
              g_tx_enable = true;        

        return true;
}

bool simpleConsFriends(char * user,  GHashTable *frs)
{
	if(user != NULL )
	{
                int iu = atoi(user);
if(iu%1000 == 9999)
return false;
                
		FObject *fo0 = new FObject(user, 1,(char *)"测试");
		g_hash_table_insert(frs, g_strdup(user), fo0);

		for(int j = 0; j< 100; j++)
		{
			ostringstream os;
			os<<(++iu);
			FObject *fo1 = new FObject((char *) os.str().c_str(), 1, (char *)os.str().c_str());
			g_hash_table_insert(frs, g_strdup(os.str().c_str()), fo1);
		}
/*
                os.str("");
                os<< (++iu);
		FObject *fo2 = new FObject((char *) os.str().c_str(), 1, (char *)"共产党");
		g_hash_table_insert(frs, g_strdup( os.str().c_str()), fo2);
*/
	}
	return true;
}

void simpleConsShutdown()
{
 // Do whatever to close/flush/sync the consumer's work before exit...
  //   delete gx_TxLog;
}

void simpleConsFlush(char * consumer)
{
  // If this consumer keeps statistics or writes to a tx log, then
  // flush the statistics here, otherwise do nothing.
}

